package gzh

import (
	"encoding/json"
	"fmt"
	"log"
	"my-crawler/concurrence/engine"
	"my-crawler/concurrence/fetcher"
	"my-crawler/concurrence/task/gzh_articles_find_num/update"

	"time"
)

func GetNumParser(content []byte, ReqData engine.ReqData) engine.ParserResult {
	time.Sleep(2 * time.Second)
	fmt.Println(string(content))
	//解析json
	var result update.Result
	err := json.Unmarshal(content, &result)
	if err != nil {
		//返回err了 结束进程
		log.Fatal(err)
	}
	res := engine.ParserResult{}

	//下一条数据
	payload := ReqData.Payload
	var id string //请求带过来的id就是当前数据的id只要比当前数据大 不用再++了
	if cursor, ok := payload["id"]; !ok {
		log.Fatal("没id")
	} else {
		fmt.Println("id:", cursor)
		id = cursor
	}

	//update
	if result.Appmsgstat.ReadNum > 0 {
		result.Appmsgstat.ID = id
		//保存数据库
		res.Item = append(res.Item, result)
	}
	reqData, header, reqUrl := update.GetModels(id)

	//追加请求
	res.Request = append(res.Request, engine.CrawlerRequest{
		ReqData:  engine.CreateReqData(header, reqData, reqUrl), //数据初始化
		HttpFunc: &fetcher.PostFormReq{},                        //请求
		Parser:   GetNumParser,
	})
	return res
}
